/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.jude.rollviewpager.adapter;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;

import com.jude.rollviewpager.ResourceTable;

import java.util.ArrayList;
import java.util.List;

/**
 * PageSliderAdapter 适配器
 *
 * @since 2021-03-23
 */
public class PageSliderAdapter extends PageSliderProvider {
    private final int mNum50 = 50;
    private final int mNum1000 = 1000;
    private final int mNum90 = 90;
    private final int mNum2500 = 2500;
    private final int mNum500 = 500;
    private final List<Integer> pages;
    private final Context context;

    /**
     * PageSliderAdapter
     *
     * @param pages pages
     * @param context context
     */
    public PageSliderAdapter(ArrayList<Integer> pages, Context context) {
        this.pages = pages;
        this.context = context;
    }

    @Override
    public int getCount() {
        return pages == null ? 0 : pages.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int post) {
        // DirectionalLayout.LayoutConfig
        ViewHolder viewHolder;
        Component component = null;

        if (component == null) {
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_pager_item, null, false);
            if (!(component instanceof ComponentContainer)) {
                return "";
            }
            componentContainer.addComponent(component);
            viewHolder = new ViewHolder();
            viewHolder.image = (Image) componentContainer.findComponentById(ResourceTable.Id_image_pager);
            component.setTag(this);
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }
        viewHolder.image.setPixelMap(pages.get(post));
        viewHolder.image.setScaleMode(Image.ScaleMode.STRETCH);

        AnimatorProperty property = viewHolder.image.createAnimatorProperty();
        property.moveFromX(mNum50).moveToX(mNum1000).rotate(mNum90).alpha(0).setDuration(mNum2500).setDelay(mNum500);
        return componentContainer;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int post, Object object) {
        if (componentContainer == null) {
            return;
        }
        if (object instanceof Component) {
            componentContainer.removeComponent((Component) object);
        }
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object obj) {
        return component == obj;
    }

    /**
     * ViewHolder
     *
     * @since 2021-03-23
     */
    public static class ViewHolder {
        private Image image;
    }
}
